function ResponceErrorHandler(service) {
    var self = this;
    Object.keys(service).forEach(function(name) {
        if (typeof(service[name]) == 'function') {
            var realFunc = service[name];
            self[name] = function () {
                if (!arguments.length) {
                    return realFunc.apply(this, arguments);
                }
                var args = [];
                for (var i = 0; i < arguments.length; i++) {
                    args.push(arguments[i]);
                }
                var index = args.length - 1;
                var cb = args[index];
                if (typeof(cb) == 'function') {
                    args[index] = function (err, data) {
                        if (err) {
                            cb(err);
                            return self.getResponse().status(500).json({error: "连接异常"});
                        }
                        cb(err, data);
                    }
                }
                return realFunc.apply(this, args);
            }
        }
    });
}
module.exports = {
    req:null,
    res:null,
    requestHandler: function (req, res, next) {
        this.req = req;
        this.res = res;
        next();
    },
    errorToResponse:function(req, res, service) {
        var ret = new ResponceErrorHandler(service);
        ret.getResponse =function() {
            return res;
        }
        return ret;
    }
}